package com.relax.netty.tcp.server;

import com.relax.netty.tcp.decode.Person;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

import java.util.Date;
import java.util.UUID;

/**
 * 泛型TextWebSocketFrame标识文本帧(frame)
 */
public class MyTcpServerHandler extends SimpleChannelInboundHandler<Person> {


    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        super.channelRead(ctx, msg);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Person data) throws Exception {
        System.out.println("服务器端接收的数据:"  + data);

//        /**
//         * 给客户端回数据
//         */
        Person person = new Person();
        person.setName("你好客户端,我是彭青松");
        person.setLength(person.getName().getBytes().length);
        ctx.writeAndFlush(person);
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

    }



    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("netty客户端 MyTcpServerHandler channelActive 执行:");
        super.channelActive(ctx);
    }
}